﻿#include"workstate/Inc/state.h"
#include<QDebug>
void ArmorState::startProgram(Work &w , Serial serial,Mat &srcBottom,Mat &srcUp)
{
     if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::ARMOR_SHOOT )
    {
        if(_taskFactory == nullptr)
        {
            _taskFactory = make_shared<ArmorShootFactory>();
            _taskOperation = _taskFactory->creatTask();
        }
        _taskOperation->setImg(srcUp,srcBottom);
        _shootPos = _taskOperation->shootCenter();
        if(_shootPos==Point2f(0,0))
        {
                uint8_t buff[17];
                buff[0] = 's';
                for(int i=1; i<16;i++)
                {
                    buff[i] = '0';
                }
                buff[16] = 'e';
                serial.WriteData(buff, sizeof(buff));
        }
        else
        {
                circle(srcUp,_shootPos,3,Scalar(0, 255, 0),2);
                serial.sendBoxPosition(_shootPos,serial,1,0);
        }
    }
    else if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::BUFF_SHOOT)
    {
       w.setState(shared_ptr<BuffState>(new BuffState()));
       w.startProgram(serial,srcBottom,srcUp);
    }
    else
    {
        w.setState(shared_ptr<NoState>(new NoState()));
        w.startProgram(serial,srcBottom,srcUp);
    }
}

void BuffState::startProgram(Work &w , Serial serial,Mat &srcBottom,Mat &srcUp)
{
    if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::BUFF_SHOOT)
    {
        if(_taskFactory == nullptr && _taskOperation == nullptr)
        {
            _taskFactory = make_shared<BuffShootFactory>();
            _taskOperation = _taskFactory->creatTask();
        }
        _taskOperation->setImg(srcUp,srcBottom);
        _shootPos = _taskOperation->shootCenter();
        if(_shootPos==Point2f(0,0))
        {
                uint8_t buff[17];
                buff[0] = 's';
                for(int i=1; i<16;i++)
                {
                    buff[i] = '0';
                }
                buff[16] = 'e';
                serial.WriteData(buff, sizeof(buff));
        }
        else
        {
                //circle(srcUp,_shootPos,3,Scalar(0, 255, 0),2);
                serial.sendBoxPosition(_shootPos,serial,1,0);
        }
    }
    else if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::ARMOR_SHOOT)
    {
        w.setState(shared_ptr<ArmorState>(new ArmorState()));
        w.startProgram(serial,srcBottom,srcUp);
    }
    else
    {
        w.setState(shared_ptr<NoState>(new NoState()));
        w.startProgram(serial,srcBottom,srcUp);
    }
    
}

void NoState::startProgram(Work &w , Serial serial,Mat &srcBottom,Mat &srcUp)
{
    if(w._task == Serial::NO_TASK)
    {
        if(_taskFactory == nullptr)
        {
            _taskFactory = make_shared<NoTaskFactory>();
            _taskOperation = _taskFactory->creatTask();
        }
    }
    else if(w._task == Serial::AUTO_SHOOT && w._shootTask == Serial::ARMOR_SHOOT)
    {
        w.setState(shared_ptr<ArmorState>(new ArmorState()));
        w.startProgram(serial,srcBottom,srcUp);
    }
    else
    {
        w.setState(shared_ptr<BuffState>(new BuffState()));
        //w.startProgram(serial,srcUp,srcBottom);
    }
}
State::State()
{
    _taskOperation = nullptr;
    _taskFactory = nullptr;

}
shared_ptr<State> Work::getState()
{
    return this->_state;
}
void Work::setState(shared_ptr<State>  s)
{
    this ->_state  = s;
}
shared_ptr<Operation> State::getTaskOperation()
{
    return this->_taskOperation;
}
